package ngames.com.sushitv.temp.Module;

import android.os.Handler;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import com.example.myvolley.MyJsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.temp.Class.Following;

/**
 * Created by edmond on 16-7-28.
 */
public class FollowingFactory {

    public void initData(AppApplication application,final List<Following> followingArrayList,final Handler handler){
        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("page","1");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");
        stringRequest.put("token",application.token);

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/subscription/user-dynamics", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("FollowingFactoryMore", String.valueOf(response));
                try {
                    JSONObject jsonObject = response.getJSONObject("data");
                    JSONArray jsonArrayUser = jsonObject.getJSONArray("users");
                    JSONArray jsonArrayVideo = jsonObject.getJSONArray("videos");
                    List<Following.User> users = JSON.parseArray(jsonArrayUser.toString(),Following.User.class);
                    List<Following.Video> videos = JSON.parseArray(jsonArrayVideo.toString(),Following.Video.class);
                    List<Following> followings = new ArrayList<>();
                    for(int i=0;i<users.size();i++){
                        Following following = new Following();
                        following.setAvatar(users.get(i).getAvatar());
                        following.setUser_id(users.get(i).getUser_id());
                        following.setCover(videos.get(i).getCover());
                        following.setDanmu_count(videos.get(i).getDanmu_count());
                        following.setPlay_count(videos.get(i).getPlay_count());
                        following.setTitle(videos.get(i).getTitle());
                        following.setV_id(videos.get(i).getV_id());
                        following.setVs_id(videos.get(i).getVs_id());
                        followings.add(following);
                    }
                    followingArrayList.clear();
                    followingArrayList.addAll(followings);
                    Log.d("FollowingFactoryInit","l"+users.size());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(0);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }

        application.queue.add(jsonObjectRequest);
    }

    public void refreshData(AppApplication application,final List<Following> followingArrayList, final Handler handler){
        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("page","1");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");
        stringRequest.put("token",application.token);

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/subscription/user-dynamics", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("FollowingFactoryRefresh", String.valueOf(response));
                try {
                    JSONObject jsonObject = response.getJSONObject("data");
                    JSONArray jsonArrayUser = jsonObject.getJSONArray("users");
                    JSONArray jsonArrayVideo = jsonObject.getJSONArray("videos");
                    List<Following.User> users = JSON.parseArray(jsonArrayUser.toString(),Following.User.class);
                    List<Following.Video> videos = JSON.parseArray(jsonArrayVideo.toString(),Following.Video.class);
                    List<Following> followings = new ArrayList<>();
                    for(int i=0;i<users.size();i++){
                        Following following = new Following();
                        following.setAvatar(users.get(i).getAvatar());
                        following.setUser_id(users.get(i).getUser_id());
                        following.setCover(videos.get(i).getCover());
                        following.setDanmu_count(videos.get(i).getDanmu_count());
                        following.setPlay_count(videos.get(i).getPlay_count());
                        following.setTitle(videos.get(i).getTitle());
                        following.setV_id(videos.get(i).getV_id());
                        following.setVs_id(videos.get(i).getVs_id());
                        followings.add(following);
                    }
                    followingArrayList.clear();
                    followingArrayList.addAll(followings);
                    Log.d("FollowingFactoryInit","l"+users.size());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(2);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }

        application.queue.add(jsonObjectRequest);
    }

    public void moreData(AppApplication application,final List<Following> followingArrayList,final Handler handler,int page){
        Log.d("FollowingFactory",page+"sss");

        Map<String,String> stringRequest = new HashMap();
        stringRequest.put("page",page+"");
        stringRequest.put("size","4");
        stringRequest.put("uuid","xxx");
        stringRequest.put("timestamp","1470104998");
        stringRequest.put("device","android");
        stringRequest.put("api_version","1.0");
        stringRequest.put("app_version","1.0");
        stringRequest.put("token",application.token);

        MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest("http://60.191.1.98:9080/v1/subscription/user-dynamics", stringRequest, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("FollowingFactoryMore", String.valueOf(response));
                try {
                    JSONObject jsonObject = response.getJSONObject("data");
                    JSONArray jsonArrayUser = jsonObject.getJSONArray("users");
                    JSONArray jsonArrayVideo = jsonObject.getJSONArray("videos");
                    List<Following.User> users = JSON.parseArray(jsonArrayUser.toString(),Following.User.class);
                    List<Following.Video> videos = JSON.parseArray(jsonArrayVideo.toString(),Following.Video.class);
                    List<Following> followings = new ArrayList<>();
                    for(int i=0;i<users.size();i++){
                        Following following = new Following();
                        following.setAvatar(users.get(i).getAvatar());
                        following.setUser_id(users.get(i).getUser_id());
                        following.setCover(videos.get(i).getCover());
                        following.setDanmu_count(videos.get(i).getDanmu_count());
                        following.setPlay_count(videos.get(i).getPlay_count());
                        following.setTitle(videos.get(i).getTitle());
                        following.setV_id(videos.get(i).getV_id());
                        following.setVs_id(videos.get(i).getVs_id());
                        followings.add(following);
                    }
                    followingArrayList.addAll(followings);
                    Log.d("FollowingFactoryInit","l"+users.size());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        if(application.queue == null){
            application.queue = Volley.newRequestQueue(application);
        }

        application.queue.add(jsonObjectRequest);
    }
}
